//
//  RegistrationDetailController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/4/20.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit

class RegistrationDetailController: BaseViewController {
    
    var isFromRecord = false
    
    var viewModel: RegistrationViewModel? {
        didSet {
            tableView.isHidden = false
            if !isFromRecord {
                confirmBtn.isHidden = false
            }
            tableView.reloadData()
        }
    }
    
    let registrationId: String
    
    init(registrationId: String) {
        self.registrationId = registrationId
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        loadData()
        // Do any additional setup after loading the view.
    }
    
    func setUI() {
        tableView.tableFooterView = UIView()
        tableView.register(RegistrationDetailLogoCell.nibForCell(),
                           forCellReuseIdentifier: RegistrationDetailLogoCell.reuseIdentifier())
        tableView.register(RegistrationDetailInfoCell.nibForCell(),
                           forCellReuseIdentifier: RegistrationDetailInfoCell.reuseIdentifier())
    }


    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.isHidden = true
        }
    }
    
    @IBOutlet weak var confirmBtn: UIButton! {
        didSet {
            confirmBtn.isHidden = true
            confirmBtn.layer.cornerRadius = confirmBtn.height / 2.0
        }
    }

    @IBAction func confirmBtnAction(_ sender: Any) {
        navigationController?.popViewController(animated: true)
    }
}

extension RegistrationDetailController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: RegistrationDetailLogoCell.reuseIdentifier(), for: indexPath) as! RegistrationDetailLogoCell
            if let vm = viewModel {
                cell.updateData(vm: vm)
            }
            return cell
        }
        if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: RegistrationDetailInfoCell.reuseIdentifier(), for: indexPath) as! RegistrationDetailInfoCell
            if let vm = viewModel {
                cell.updateForRegistrationDetail(vm: vm)
            }
            return cell
        }
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 190
        }
        if indexPath.row == 1 {
            return UITableView.automaticDimension
        }
        return 0
    }

}

extension RegistrationDetailController {
    private func loadData() {
        RegistrationViewModel.registrationDetail(scheduleId: self.registrationId
            , success: { (model) in
            self.viewModel = RegistrationViewModel(model: model)
        }) { (msg) in
            HUDTool.showError(message: msg)
        }
    }
}
